package com.issoft.studentresitor.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.pojo.Student;
import com.issoft.studentresitor.domain.query.StudentQueryParam;
import com.issoft.studentresitor.domain.vo.StudentVo;
import com.issoft.studentresitor.mapper.StudentMapper;
import com.issoft.studentresitor.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.issoft.studentresitor.utils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<StudentVo> queryStuPage(StudentQueryParam studentQueryParam) {
        log.info("分页查询学生信息");
        log.info("分页查询参数：{}", studentQueryParam);
        //设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        //执行查询
        List<StudentVo> studentVos = studentMapper.queryStuPage(studentQueryParam);
        //3.封装
        Page<StudentVo> pageResult =(Page<StudentVo>) studentVos;
        return new PageResult<>(pageResult.getTotal(), pageResult.getResult());
    }

    @Override
    public Result export(HttpServletResponse response) throws IOException {
        //1.查询所有
        List<Student> students = studentMapper.selectList( null);
        log.info("导出数据：{}", students);
        //通过工具类创建writer,默认创建xls格式
        ExcelWriter excelWriter = ExcelUtil.getWriter(true);
        excelWriter.write(students, true);
        //设置浏览器响应格式导出文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String fileName = URLEncoder.encode("学生信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        //获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        excelWriter.flush(outputStream, true);
        excelWriter.close();
        outputStream.close();
        return Result.success();
    }

    @Override
    public List<StudentVo> queryNoClassStu() {
        return studentMapper.queryNoClassStu();
    }

    @Override
    public List<Student> queryNoDromStu() {
        return studentMapper.queryNoDromStu();
    }

    @Override
    public List<Student> getByUIdQuerStudent(Integer userId) {
        return studentMapper.getByUIdQuerStudent(userId);
    }

    @Override
    public List<Student> queryDromStu(Integer userId) {
        return studentMapper.queryDromStu(userId);
    }

    @Override
    public List<Student> queryNoUserIdStu() {
        return studentMapper.queryNoUserIdStu();
    }

    @Override
    public int addUserId(Integer id, Integer userId) {
        int i = studentMapper.addUserId(id, userId);
        return i;
    }

    @Override
    public boolean updateStuInfo(@Param("userId") Integer userId,@Param("studentId")  String studentId,@Param("sex")  Integer sex,@Param("contactPhone")  String contactPhone,@Param("emergencyContact") String emergencyContact,@Param("emergencyPhone") String emergencyPhone) {
        boolean b = studentMapper.updateStuInfo(userId, studentId, sex, contactPhone, emergencyContact, emergencyPhone);
        return b;
    }

    @Override
    public int updateContact(Integer userId, String emergencyContact, String emergencyPhone, String contactPhone) {
        int i = studentMapper.updateContact(userId, emergencyContact, emergencyPhone, contactPhone);
        return i;
    }

    @Override
    public void updateStuPhoto(Integer userId, String url) {
        studentMapper.updateStuPhoto(userId, url);
        log.info("更新学生头像成功");
    }

    @Override
    public int updateInfo(Student student) {
        int i = studentMapper.updateInfo(student);
        return i;
    }
}
